import { _decorator, Node } from 'cc';
import { BaseComponent } from '../../Manager/UIManager/BaseComponent';
import { GAME_POP, GAME_SCENE, GAME_TOP, GlobalData, LevelConfig } from '../../Data/GlobalData';
import { CarLoad } from './CarLoad/CarLoad';
import { GunLoad } from './GunLoad/GunLoad';
import { DragonLoad } from './DragonLoad/DragonLoad';
import { ButtleLoad } from './ButtleLoad/ButtleLoad';
import { BluttonLoad } from './BluttonLoad';
import { PopAd } from '../../Pop/PopAd';
import { TopPromptText } from '../../Top/TopPromptText';
const { ccclass, property } = _decorator;

@ccclass('GameScene')
/**
 * 游戏场景
 */
export class GameScene extends BaseComponent {

    //汽车层
    @property(Node)
    private CarLoad: Node = null;

    //炮台层
    @property(Node)
    private GunLoad: Node = null;

    //龙层
    @property(Node)
    private DragonLoad: Node = null;

    //子弹层
    @property(Node)
    private ButtleLoad: Node = null;

    //按钮层
    @property(Node)
    private ButtonLoad: Node = null;

    //是否播放广告复活
    public IsAd: boolean = false;

    protected Init(): void {

    }

    protected OnShow(): void {
        this.IsAd = true;
        GlobalData.CurrentLevel = app.gameData.GameData.currentlevel;
        // GlobalData.CurrentLevel = 0;
        this.ButtleLoad.getComponent(ButtleLoad).Init();
        this.GameStart();
    }

    /**
     * 开始游戏
     */
    public GameStart() {

        GlobalData.DragonBodyInfo = [];

        GlobalData.GameOver = false;

        GlobalData.BodyAddSpeed = 0;

        GlobalData.ButtleSpeed = 10;
        GlobalData.IsHit = true;

        this.ButtonLoad.getComponent(BluttonLoad).LevelLabel.string = "d" + (GlobalData.CurrentLevel + 1) + "g";

        this.CarLoad.getComponent(CarLoad).UpdateLevelInfo();
        this.GunLoad.getComponent(GunLoad).ClearAllGun();
        this.DragonLoad.getComponent(DragonLoad).UpdateLevelInfo();
    }

    /**
     * 游戏过关
     */
    public GamePass() {

        if (GlobalData.CurrentLevel == LevelConfig.length - 1) {
            app.uiManager.ShowUI(GAME_SCENE.LoginScene);
            app.topManager.ShowTop(GAME_TOP.TopPromptText, false);
            app.topManager.CurrentTop.getComponent(TopPromptText).UpdatePrompt("恭喜你通关了！");
            return;
        }

        GlobalData.CurrentLevel++;

        //更新本地关卡缓存
        if (GlobalData.CurrentLevel > app.gameData.GameData.currentlevel) {
            app.gameData.GameData.currentlevel = GlobalData.CurrentLevel
            app.gameData.UpdateGameDateLocal();
        }

        this.GameStart();
    }

    /**
     * 游戏结束
     */
    public GameOver() {

        GlobalData.GameOver = true;

        if (this.IsAd) {
            this.IsAd = false;
            app.popManager.ShowPop(GAME_POP.PopAd)
            app.popManager.CurrentPop.getComponent(PopAd).SaveCb = () => {
                GlobalData.GameOver = false;
                this.DragonLoad.getComponent(DragonLoad).IsSave = true;
                this.DragonLoad.getComponent(DragonLoad).SaveTime = 2;
                app.popManager.HidePop(GAME_POP.PopAd);
            }

            app.popManager.CurrentPop.getComponent(PopAd).CloseCb = () => {
                app.popManager.HidePop(GAME_POP.PopAd);
                this.GameOver();
            }
            return;
        }

        app.popManager.ShowPop(GAME_POP.PopEnd);

    }

}